//
//  AgainBellView.m
//  AlarmClock
//
//  Created by apple on 16/9/30.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "AgainBellView.h"
#import "SDAutoLayout.h"
@implementation AgainBellView
- (void)show:(CGRect)rect againValue:(NSInteger)again countValue:(NSInteger)count {
    [self addSubview:self.backView];
    self.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.51];
    self.backView.frame = rect;
    self.backView.backgroundColor = [UIColor whiteColor];
   
    [self.backView addSubview:self.titleLabel];
    [self.backView addSubview:self.timeLabel];
    [self.backView addSubview:self.timeSlider];
    [self.backView addSubview:self.countLabel];
    [self.backView addSubview:self.countSlider];
    [self.backView addSubview:self.sureButton];
    [self.backView addSubview:self.cancelButton];
    [self.backView addSubview:self.lineView];
    
    self.titleLabel.sd_layout
    .topSpaceToView(self.backView,10)
    .leftSpaceToView(self.backView,0)
    .widthIs(rect.size.width)
    .heightIs(30);
    
    self.timeLabel.sd_layout
    .topSpaceToView(self.titleLabel,10)
    .leftSpaceToView(self.backView,20)
    .widthIs(rect.size.width)
    .heightIs(30);
    
    self.timeSlider.sd_layout
    .topSpaceToView(self.timeLabel,0)
    .leftSpaceToView(self.backView,20)
    .widthIs(rect.size.width - 40)
    .heightIs(30);
    
    self.lineView.sd_layout
    .leftSpaceToView(self.backView,0)
    .topSpaceToView(self.backView,rect.size.height / 2.2)
    .widthIs(rect.size.width)
    .heightIs(1.5);
    
    self.countLabel.sd_layout
    .topSpaceToView(self.lineView,20)
    .leftSpaceToView(self.backView,20)
    .widthIs(rect.size.width)
    .heightIs(30);
    
    self.countSlider.sd_layout
    .topSpaceToView(self.countLabel,0)
    .leftSpaceToView(self.backView,20)
    .widthIs(rect.size.width - 40)
    .heightIs(30);
    
    
    self.cancelButton.sd_layout
    .leftSpaceToView(self.backView,20)
    .widthIs((rect.size.width - 50)/2)
    .bottomSpaceToView(self.backView,10)
    .heightIs(rect.size.width / 6);
    
    self.sureButton.sd_layout
    .bottomSpaceToView(self.backView,10)
    .leftSpaceToView(self.cancelButton,10)
    .widthIs((rect.size.width - 50)/2)
    .heightIs(rect.size.width / 6);
    
    if (again != 0 ) {
        self.timeSlider.value = again / 5;
        if (count == 10) {
            self.countSlider.value = 4;
        } else {
           self.countSlider.value = count / 2 + 1;
        }
        
    }
    for (int i = 0 ; i < 6; i++) {
        UILabel *label = [[UILabel alloc]init];
        label.text = [NSString stringWithFormat:@"%d",(i+1) * 5];
        [self.backView addSubview:label];
        if (i == 0) {
            label.sd_layout
            .leftSpaceToView(self.backView,20)
            .topSpaceToView(self.timeSlider,0)
            .widthIs((rect.size.width - 40)/6)
            .heightIs(25);
        } else {
            label.sd_layout
            .leftSpaceToView(self.backView,rect.size.width/6 * i +20)
            .topSpaceToView(self.timeSlider,0)
            .widthIs((rect.size.width - 40)/6)
            .heightIs(25);
        }
    }
    
    for (int i = 0 ; i < 4; i++) {
        UILabel *label = [[UILabel alloc]init];
        if (i == 3) {
            label.text = @"10";
        } else {
           label.text = [NSString stringWithFormat:@"%d",(i * 2) + 1]; 
        }
        [self.backView addSubview:label];
        if (i == 0) {
            label.sd_layout
            .leftSpaceToView(self.backView,33)
            .topSpaceToView(self.countSlider,0)
            .widthIs((rect.size.width - 40)/4)
            .heightIs(25);
        } else {
            label.sd_layout
            .leftSpaceToView(self.backView,rect.size.width/4 * i +33)
            .topSpaceToView(self.countSlider,0)
            .widthIs((rect.size.width - 40)/4)
            .heightIs(25);
        }
    }

    [UIView animateWithDuration:0.4 animations:^{
        self.transform = CGAffineTransformMakeScale(1, 1);
        self.backView.alpha = 1;
    }];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [self addGestureRecognizer:tap];
    UITapGestureRecognizer * emptyTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(emtyAction:)];
    [self.backView addGestureRecognizer:emptyTap];
}

-(void)emtyAction:(UITapGestureRecognizer*)gesture{

}

-(void)tapAction:(UITapGestureRecognizer*)gesture{
    [UIView animateWithDuration:0.3 animations:^{
        self.backView.transform = CGAffineTransformMakeScale(0.1, 0.1);
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


#pragma mark - action
- (void)cancelButtonPressed:(UIButton *)sender {
    [UIView animateWithDuration:0.3 animations:^{
        self.backView.transform = CGAffineTransformMakeScale(0.1, 0.1);
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)sureButtonPressed:(UIButton *)sender {
    if (_againBellViewHandle) {
        _againBellViewHandle(self.timeSlider.value,self.countSlider.value);
    }
    [UIView animateWithDuration:0.2 animations:^{
        self.backView.transform = CGAffineTransformMakeScale(0.1, 0.1);
        self.alpha = 0;
    }];
}

- (void)timeSliderProgress:(UISlider *)slider {
    self.timeSlider.value = round(slider.value);
}

- (void)countSliderProgress:(UISlider *)slider {
    self.countSlider.value = round(slider.value);
}
#pragma mark - getter
- (UILabel *)titleLabel {
    if (_titleLabel) {
        return _titleLabel;
    }
    _titleLabel = [[UILabel alloc]init];
    _titleLabel.text = @"再响设置";
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    return _titleLabel;
}

- (UISlider *)timeSlider {
    if (_timeSlider) {
        return _timeSlider;
    }
    _timeSlider = [[UISlider alloc]init];
    _timeSlider.minimumTrackTintColor = [UIColor colorWithRed:0.5607 green:0.8418 blue:0.826 alpha:1.0];
    _timeSlider.maximumValue = 6;
    _timeSlider.minimumValue = 1;
    _timeSlider.value = 1;
    [_timeSlider addTarget:self action:@selector(timeSliderProgress:) forControlEvents:UIControlEventValueChanged];
    return _timeSlider;
}

- (UILabel *)timeLabel {
    if (_timeLabel) {
        return _timeLabel;
    }
    _timeLabel = [[UILabel alloc]init];
    _timeLabel.text = @"响铃间隔时间(分钟)";
    return _timeLabel;
}

- (UILabel *)countLabel {
    if (_countLabel) {
        return _countLabel;
    }
    _countLabel = [[UILabel alloc]init];
    _countLabel.text = @"重复响铃次数";
    return _countLabel;
}

- (UISlider *)countSlider {
    if (_countSlider) {
        return _countSlider;
    }
    _countSlider = [[UISlider alloc]init];
    _countSlider.minimumTrackTintColor = [UIColor colorWithRed:0.5607 green:0.8418 blue:0.826 alpha:1.0];
    [_countSlider addTarget:self action:@selector(countSliderProgress:) forControlEvents:UIControlEventValueChanged];
    _countSlider.maximumValue = 4;
    _countSlider.minimumValue = 1;
    _countSlider.value = 1;
    return _countSlider;
}

- (UIView *)lineView {
    if (_lineView) {
        return _lineView;
    }
    _lineView = [[UIView alloc]init];
    _lineView.backgroundColor = [UIColor grayColor];
    return _lineView;
}

- (UIButton *)cancelButton {
    if (_cancelButton) {
        return _cancelButton;
    }
    _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _cancelButton = [[UIButton alloc]init];
    _cancelButton.layer.masksToBounds = YES;
    _cancelButton.layer.cornerRadius = 10;
    [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [_cancelButton addTarget:self action:@selector(cancelButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    _cancelButton.backgroundColor = [UIColor redColor];
    return _cancelButton;
}

- (UIButton *)sureButton {
    if (_sureButton) {
        return _sureButton;
    }
    _sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _sureButton = [[UIButton alloc]init];
    _sureButton.layer.masksToBounds = YES;
    _sureButton.layer.cornerRadius = 10;
    [_sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [_sureButton addTarget:self action:@selector(sureButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    _sureButton.backgroundColor = [UIColor redColor];
    return _sureButton;
}

- (UIView *)backView {
    if (_backView) {
        return _backView;
    }
    _backView = [[UIView alloc]init];
    _backView.layer.masksToBounds = YES;
    _backView.layer.cornerRadius = 10;
    _backView.alpha = 0;
    return _backView;
}
@end
